function A = buildgraph(X,D)
XX = sum(X.*X,2);
n = size(XX,1);
A = repmat(XX,1,n)+repmat(XX',n,1)-X*X';
A = A/max(max(A));
A = (A < D);
A = sparse(A);
%{
for i = 1:n
    for j = 1:i-1
        if((XX(i)+XX(j)-2*X(i,:)'*X(j,:))<=D)
            idx = [idx;i];
            jdx = [jdx;j];
        end
    end
end
A = sparse(idx,jdx,1);
e = size(idx,1);
%}
end